package com.cskaoyan.controller.admin.system;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.exception.InvalidParamException;
import com.cskaoyan.service.admin.system.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author erdonc
 * @date 2022/9/6 22:08
 * @description: TODO 管理员管理模块：URL : admin/admin/*
 */
@RestController
@RequestMapping("admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * @description: TODO 根据管理员名称进行模糊查询
     * @date: 2022/9/7 15:18
     * @param: [basePageInfo(分页信息), name（管理员名称）]
     */
    // @RequiresPermissions("admin:admin:list")
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo, String username) {
        CommonData<MarketAdmin> adminList = adminService.list(basePageInfo, username);
        return BaseRespVo.ok(adminList);
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/12 22:07
     * TODO:  添加管理员
     */
    // @RequiresPermissions("admin:admin:create")
    @RequestMapping("create")
    // @OperationLog(action = "添加管理员")
    public BaseRespVo create(@RequestBody MarketAdmin admin) throws Exception {
        checkNameAndPwd(admin.getUsername(), admin.getPassword());
        MarketAdmin creatVo = adminService.create(admin);
        return BaseRespVo.ok(creatVo);
    }

    /**
     * @description: TODO 校验名称和密码的合法性
     * @date: 2022/9/7 16:34
     * @param: [username, password]
     */
    private void checkNameAndPwd(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new InvalidParamException("用户名或密码不能为空！");
        }
    }


    /**
     * @description: TODO 修改管理员
     * @date: 2022/9/8 14:57
     * @param: [admin]
     */
    // @RequiresPermissions("admin:admin:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketAdmin admin) throws Exception {
        checkNameAndPwd(admin.getUsername(), admin.getPassword());
        MarketAdmin updateAdmin = adminService.update(admin);
        return BaseRespVo.ok(updateAdmin);
    }

    /**
     * @description: TODO 删除管理员
     * @date: 2022/9/8 16:30
     * @param: [admin]
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketAdmin admin) {
        adminService.delete(admin);
        return BaseRespVo.ok(null);
    }

}
